package com.open.server.api.system;

import com.open.system.dispatch.ILogDispatch;
import com.open.system.model.Log;
import com.open.common.model.Page;
import com.open.common.model.PageRequest;
import com.open.common.model.Result;
import com.open.system.model.vo.LogVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统日志
 * @author 大树03
 * @date 2025-07-04 11:16
 */
@RestController
@RequestMapping("/system/log")
public class LogApi{

    @Autowired
    private ILogDispatch logDispatch;

    /**
     * 对象添加
     * @param model 添加的对象
     * @return
     */
    @PostMapping("save")
    public Result <Log> save(@RequestBody Log model)  {
        long now=System.currentTimeMillis();
        Result<Log> result=new Result<>();
        logDispatch.save(model);
        result.setData(model);
        result.setTime(System.currentTimeMillis()-now);
        return result;
    }

    /**
     * 对象修改
     * @param model 修改的对象
     * @return
     */
    @PostMapping("update")
    public Result<Boolean> update(@RequestBody Log model)  {
        long now=System.currentTimeMillis();
        Result<Boolean> result=new Result<>();
        result.setData(logDispatch.update(model));
        result.setTime(System.currentTimeMillis()-now);
        return result;
    }

    /**
     * 根据对象列表查询
     * @param model 查询条件
     * @return
     */
    @PostMapping("findByModel")
    public Result<List<Log>> findByModel(@RequestBody Log model)  {
        long now=System.currentTimeMillis();
        Result<List<Log>> result=new Result<>();
        result.setData(logDispatch.findByModel(model));
        result.setTime(System.currentTimeMillis()-now);
        return result;
    }

    /**
     * 根据主键查询对象
     * @param id 查询的对象主键
     * @return
     */
    @GetMapping("findByPk")
    public Result<Log> findByPk(@RequestParam("id") Long id)  {
        long now=System.currentTimeMillis();
        Result<Log> result=new Result<>();
        result.setData(logDispatch.findByPk(id));
        result.setTime(System.currentTimeMillis()-now);
        return result;
    }

    /**
     * 分页查询
     * @param pageRequest 分页查询条件
     * @return
     */
    @PostMapping("findPage")
    public Result<Page<LogVo>> findPage(@RequestBody  PageRequest<Log> pageRequest)  {
        long now=System.currentTimeMillis();
        Result<Page<LogVo>> result=new Result<>();
        result.setData(logDispatch.findPage(pageRequest.getPage(), pageRequest.getModel()));
        result.setTime(System.currentTimeMillis()-now);
        return result;
    }

    /**
     * 根据主键删除对象
     * @param ids 删除的对象主键
     * @return
     */
    @GetMapping("remove")
    public Result<Boolean> remove(@RequestParam("ids") List<Long> ids)  {
        long now=System.currentTimeMillis();
        Result<Boolean> result=new Result<>();
        result.setData(logDispatch.remove(ids));
        result.setTime(System.currentTimeMillis()-now);
        return result;
    }

}